<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Models\Auth\User;
use App\Models\Common\ApiResponse;
use Illuminate\Http\Request;

class AuthController extends Controller
{

    public $name = '授权管理';

    public function login(Request $request): array
    {
        if (!$userName = $request->get('userName')) {
            throw new \InvalidArgumentException('账号必填', ApiResponse::CODE_INVALID_ARGUMENT);
        }

        if (!$password = $request->get('password')) {
            throw new \InvalidArgumentException('密码必填', ApiResponse::CODE_INVALID_ARGUMENT);
        }

        if (!$user = User::where('userName', $userName)->first()) {
            throw new \InvalidArgumentException('账号或密码错误', ApiResponse::CODE_INVALID_ARGUMENT);
        }

        if (!$JwtToken = auth('admin')->attempt(compact('userName', 'password'))) {
            return ApiResponse::return('登录账号或密码错误', '', ApiResponse::RESP_CODE_FAILURE);
        }

        try {
            $user->lst_login_at = now()->toDateTimeString();
            $user->lst_login_ip = ip2long($request->getClientIp());
            $user->save();
        } catch (\Throwable $exception) {
        }
        $this->admin = $user;
        $this->logContent = '登入系统后台';

        return ApiResponse::return('', ['token' => $JwtToken]);
    }

    public function logout(Request $request): array
    {
        $this->admin = admin();
        \JWTAuth::invalidate();
        $this->logContent = '登出系统';
        return ApiResponse::return('成功退出登录');
    }

    public function resetPassword(Request $request)
    {
        if ($user = admin()) {
            if (!$password = $request->post('password')) {
                return ApiResponse::return('登录密码不可为空', '', ApiResponse::CODE_INVALID_ARGUMENT);
            }
            if (strlen($password) < 6) {
                throw new \InvalidArgumentException('登录密码长度至少为6位', ApiResponse::CODE_INVALID_ARGUMENT);
            }

            $admin = User::find($user->id);
            $admin->update(['password' => bcrypt($password)]);

            if (\JWTAuth::getToken()) {
                \JWTAuth::invalidate();
            }

            $this->admin = $user;
            $this->logContent = '重置密码';

            return ApiResponse::return('修改成功，请重新登录');
        } else {
            return ApiResponse::return('未登录');
        }
    }

    public function merchantLogin(Request $request): array
    {
        if (!$userName = $request->get('userName')) {
            throw new \InvalidArgumentException('账号必填', ApiResponse::CODE_INVALID_ARGUMENT);
        }

        if (!$password = $request->get('password')) {
            throw new \InvalidArgumentException('密码必填', ApiResponse::CODE_INVALID_ARGUMENT);
        }

        if (!$user = User::where('userName', $userName)->first()) {
            throw new \InvalidArgumentException('账号或密码错误', ApiResponse::CODE_INVALID_ARGUMENT);
        }
        if (User::IDENTITY_MERCHANT !== $user->identity) {
            throw new \InvalidArgumentException('非商户账号', ApiResponse::CODE_INVALID_ARGUMENT);
        }

        if (!$JwtToken = auth('admin')->attempt(compact('userName', 'password'))) {
            return ApiResponse::return('登录账号或密码错误', '', ApiResponse::RESP_CODE_FAILURE);
        }

        try {
            $user->lst_login_at = now()->toDateTimeString();
            $user->lst_login_ip = ip2long($request->getClientIp());
            $user->save();
        } catch (\Throwable $exception) {
        }
        $this->admin = $user;
        $this->logContent = '登入商户账号';

        return ApiResponse::return('', ['token' => $JwtToken]);
    }
}
